/*
 *  Copyright 2011 derek.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.aocore.packet.CS;

import com.aocore.AoCoreProperties;
import com.aocore.d2data.RealmLogonResult;
import com.aocore.packet.CSPacket;
import com.aocore.util.ByteConverter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 *
 * @author derek
 */
public class RealmLogonResponse extends CSPacket
{
    protected int cookie;
    protected RealmLogonResult result;
    protected int realmServerIP = -1;
    protected int realmServerPort = -1;
    protected String username = null;

    public RealmLogonResponse(byte... data)
    {
        super(data);

        ByteBuffer leByteBuffer = ByteBuffer.wrap(data);
        ByteBuffer beByteBuffer = ByteBuffer.wrap(data);
        leByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        beByteBuffer.order(ByteOrder.BIG_ENDIAN);
        this.cookie = leByteBuffer.getInt(4);
        if (this.data.length < 75)
        {
            this.result = RealmLogonResult.valueOf(leByteBuffer.getInt(8));
            return;
        }
        this.result = RealmLogonResult.Success;

        // 3 * DWORD = MCP Chunk DWORDS 2 - 4
        this.realmServerIP = leByteBuffer.getInt(20);
        this.realmServerPort = beByteBuffer.getShort(24);
        // 00, 00 (port is actually a DWORD...)
        // 12 * DWORD = MCP Chunk DWORDS 5 - 16
        this.username = ByteConverter.getString(data, 76);


        ip = new byte[] { beByteBuffer.get(20), beByteBuffer.get(21), beByteBuffer.get(22), beByteBuffer.get(23) };
        port = beByteBuffer.getShort(24);
        hash = new byte[64];

        beByteBuffer.position(4);
        beByteBuffer.get(hash, 0, 16);
        beByteBuffer.position(28);
        beByteBuffer.get(hash, 16, 48);

        // change address
        beByteBuffer.putInt(20, AoCoreProperties.ADDRESS);
        beByteBuffer.putShort(24, AoCoreProperties.REALM_PORT);
    }

    //TODO MAKE ALL NICE
    byte[] ip;
    short port;
    byte[] hash;

    public byte[] getIp()
    {
        return ip;
    }

    public short getPort()
    {
        return port;
    }

    public byte[] getHash()
    {
        return hash;
    }

    @Override
    public String toString()
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("CSPacket: ").append("RealmLogonResponse").append("\n");
        stringBuilder.append("cookie: ").append(cookie).append("\n");
        stringBuilder.append("result: ").append(result).append("\n");
        stringBuilder.append("realmServerIP: ").append(realmServerIP).append("\n");
        stringBuilder.append("realmServerPort: ").append(realmServerPort).append("\n");
        stringBuilder.append("username: ").append(username).append("\n");
        return stringBuilder.toString();
    }
}
